<template>
  <div class="news-page">
    <TopBar />
    <div class="content">
      <MenuLine />

      <div class="news-section">
        <div class="news-header">
          <h2>新闻资讯</h2>
        </div>
        <div
          v-infinite-scroll="loadMore"
          infinite-scroll-distance="100"
          infinite-scroll-disabled="scrollDisabled"
          class="news-list"
        >
          <el-row :gutter="30">
            <el-col
              v-for="news in newsList"
              :key="news.id"
              :span="8"
              class="news-card"
              @click="$router.push(`/news/${news.id}`)"
            >
              <el-card :body-style="{ padding: '0px' }">
                <img
                  :src="news.coverUrl || defaultImage"
                  class="news-image"
                  @error="handleImageError"
                />
                <div class="news-title">
                  <span>{{ news.title }}</span>
                </div>
              </el-card>
            </el-col>
          </el-row>
          <div v-if="loading" class="loading">加载中...</div>
          <div v-if="!loading && noMore" class="no-more">没有更多数据了</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useNewsStore } from '@/stores/news';
import TopBar from '@/components/TopBar.vue';
import MenuLine from '@/components/MenuLine.vue';

const router = useRouter();
const newsStore = useNewsStore();

const defaultImage = 'https://picsum.photos/400/200?random=0';
const newsList = ref([]);
const loading = ref(false);
const noMore = ref(false);
const pageNum = ref(1);
const pageSize = 9;

const scrollDisabled = computed(() => loading.value || noMore.value);

const handleImageError = (e) => {
  e.target.src = defaultImage;
};

const loadMore = async () => {
  if (loading.value || noMore.value) return;

  loading.value = true;
  try {
    const response = await newsStore.fetchNewsList();
    const newData = response.slice((pageNum.value - 1) * pageSize, pageNum.value * pageSize);

    if (newData.length > 0) {
      newsList.value.push(...newData);
      pageNum.value++;
    } else {
      noMore.value = true;
    }
  } catch (error) {
    ElMessage.error('加载新闻失败: ' + error.message);
  } finally {
    loading.value = false;
  }
};

onMounted(async () => {
  await loadMore();
});
</script>

<style scoped>
.news-page {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.content {
  padding-top: 60px; /* 留出顶栏高度 */
  width: 80%;
  margin: 0 auto;
  padding-bottom: 60px; /* 底部留白 */
}

.news-section {
  margin: 20px 0;
}

.news-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.news-header h2 {
  font-size: 24px;
  color: #333;
  flex-grow: 1;
  text-align: center;
}

.news-list {
  min-height: 100vh;
}

.news-card {
  margin-bottom: 20px;
  cursor: pointer;
}

.news-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.news-title {
  padding: 14px;
  min-height: 50px;
  display: flex;
  align-items: center;
}

.news-title span {
  font-size: 14px;
  color: #333;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}

.loading,
.no-more {
  text-align: center;
  padding: 20px;
  color: #666;
  font-size: 14px;
}
</style>